﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VectorHelper
{
    /// <summary>
    /// 向量常用运算类
    /// </summary>
    public class Func
    {
        /// <summary>
        /// 设置向量AB(其中A(x1,y1), B(x2,y2))
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        /// <returns></returns>
        public static Vector SetVector(int x1, int y1, int x2, int y2)
        {
            Vector vector = new Vector(x2 - x1, y2 - y1);
            return vector;
        }

        /// <summary>
        /// 设置向量AB(其中A(x1,y1), B(x2,y2))
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        /// <returns></returns>
        public static Vector SetVector(float x1, float y1, float x2, float y2)
        {
            Vector vector = new Vector(x2 - x1, y2 - y1);
            return vector;
        }

        /// <summary>
        /// a + b 向量相加
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns>c</returns>
        public static Vector Add(Vector a, Vector b)
        {
            Vector c = new Vector(a.X + b.X, a.Y + b.Y);
            return c;
        }

        /// <summary>
        /// a - b 向量相减
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns>c</returns>
        public static Vector Sub(Vector a, Vector b)
        {
            Vector c = new Vector(a.X - b.X, a.Y - b.Y);
            return c;
        }

        /// <summary>
        /// |a| 向量求模
        /// </summary>
        /// <param name="vector"></param>
        /// <returns></returns>
        public static double Mod(Vector vector)
        {
            return Math.Sqrt(vector.X * vector.X + vector.Y * vector.Y);
        }

        /// <summary>
        /// -1*a 向量取反
        /// </summary>
        /// <param name="vector"></param>
        /// <returns></returns>
        public static Vector Negate(Vector vector)
        {
            vector.X *= -1;
            vector.Y *= -1;
            return vector;
        }


        /// <summary>
        /// a·b 点乘、数量积 (a.X * b.X + a.Y * b.Y)
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double DotProduct(Vector a, Vector b)
        {
            return a.X * b.X + a.Y * b.Y;
        }


       



        public static Vector Move(Vector vector, float rete)
        {
            Vector new_m = new Vector(vector.X, vector.Y);
            //标量化
            double m_mod = Mod(new_m);
            new_m.X /= (float)m_mod;
            new_m.Y /= (float)m_mod;
            //乘上移动速度
            new_m.X *= rete;
            new_m.Y *= rete;
            //改变移动后的坐标
            vector.X += new_m.X;
            vector.Y += new_m.Y;
            return vector;
        }

        /// <summary>
        /// 两向量间夹角的余弦值 a·b = |a|·|b|·COSα
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static double GetAngle(Vector a, Vector b)
        {
            var ab = DotProduct(a, b);//数量积
            var a_mod = Mod(a);
            var b_mod = Mod(b);
            var c = a_mod * b_mod;
            return ab / c;
        }

    }
}
